{% extends 'app01/base_backend.html' %}
{% block person_manage %}
    <a class="navbar-brand" href="#">web资源分类</a>
{% endblock %}
{% block all_li %}
    <li role="presentation">
        <a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">web资源分类</a>
    </li>
    <li role="presentation" class="active">
        <a href="{% url 'app01_backend' %}" style="color: #337ab7">返回个人管理</a></li>
{% endblock %}

{% block all_content %}
    {% block caogao %}
        <div>
            <table class="table table-striped table-hover">
                <thead>
                <tr>
                    <th>资源分类名</th>
                    <th>资源描述</th>
                    <th>创建时间</th>
                </tr>
                </thead>
                <tbody>
                {% for file in soures %}
                    <tr>
                        <td>
                            {{ file.name }}
                        </td>
                        <td>{{ file.descn }}</td>
                        <td>{{ file.create_time }}</td>
                    </tr>
                {% empty %}
                    <h4>您还没有创建资源分类</h4>
                {% endfor %}
                </tbody>

            </table>
        </div>

        <hr>
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    资源分类
                    <input type="text" class="form-control" id="web_cate_id">
                    <br>
                    资源描述
                    <input type="text" class="form-control" id="web_desc_id">
                    <br>
                    <button class="btn btn-success " id="add_web_id">点击创建</button>

                </div>
            </div>
        </div>

    {% endblock %}

{% endblock %}
{% block js %}
    <script>
        if ('{{ request.user.username }}'.slice(0, 3) === '512') {
            $('#p_for_a').html('<a href="/app01/manage_student/" class="list-group-item">学生管理</a>')
            $('#role_for').html('角色：<br><a href="#">老师</a>')
        }


        if ('{{ request.user.username }}'.slice(0, 3) === '312') {

            $('#set_person_id').html('<p>班级：<input type="text" id="stu_class" class="form-control" value="{{ request.user.stu_class }}"></p>')
            $('#role_for').html('角色：<br><a href="#">学生</a>')
        }

        //聚焦,点击清空内容


        //创建资源分类的ajax
        $('#add_web_id').click(function () {
            let source_name = $('#web_cate_id').val()
            let sourse_desc = $('#web_desc_id').val()

            let desc = $('#web_desc_id').val()
            let web_cate = $('#web_cate_id').val()
            if (desc == null) {
                alert('描述信息不能为空')
            } else if (web_cate == null) {
                alert('分类名不能为空')
            } else {
                //只有两个都不为空的时候才能发起ajax请求
                  $.ajax({
                url: '',
                type: 'post',
                data: {'source_name': source_name, 'source_desc': sourse_desc},
                success: function (attr) {
                    if (attr.code === 100) {
                        alert(attr.msg)
                        window.location.reload()
                    } else {
                        alert(attr.msg)
                    }

                }
            })
            }




        })

     //文件上传的ajax请求
        $("#up_btn_id").click(function () {
            let formObj = new FormData()
            formObj.append('file', $('#up_file_id')[0].files[0])
            formObj.append('filename', $('#file_name_id').val())
            formObj.append('desc', $('#file_desc_id').val())
            let radio_chioce = $('input[name="share"]:checked').val();
            formObj.append('share', radio_chioce)

            $.ajax({
                url: "/app01/upload_file/",
                type: 'post',
                data: formObj,
                contentType: false,
                processData: false,
                success: function (array) {
                    if (array.code === 1000) {
                        alert(array.msg0)
                        parent.location.href = array.url
                    } else if (array.code === 2000) {
                        //用户输入不正确的提示
                        $('#p1').next().text(array.msg1)
                        $('#p2').next().text(array.msg2)
                        $('#p3').next().text(array.msg3)
                    } else {
                        alert(array.msg0)
                    }

                }
            })


        });

    </script>
{% endblock %}